package com.netnovelstore.pojo;

import com.alibaba.fastjson2.JSON;
import com.netnovelstore.pojo.Exception.NotDataException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "用户实体")
public class User implements Serializable {
    @NotNull(message = "userNumber不能为空")
    private Integer userNumber;

    private String userName;

    private String password;
    private String userType;
    private String userImg;
    private String nickName;
    private Map<String,Book> bookshelf;
    @ApiModelProperty(name = "unread", value = "未读信息")
    private List<Message> unread;
    @ApiModelProperty(name = "read", value = "已读信息")
    private List<Message> read;
    private Integer level;
    private Integer balance;
    private List<String> hobby;
    private Map<String,Book> history;



    public void setUserName(String userName) {
        if(! userName.isEmpty())
            this.userName = userName;
    }

    public void setPassword(String password) {
        if(! password.isEmpty())
            this.password = password;
    }

    public void setUserType(String userType) {
        if(! userType.isEmpty())
            this.userType = userType;
    }

    public void setUserImg(String userImg) {
        if(! userImg.isEmpty())
            this.userImg = userImg;
    }

    public void setNickName(String nickName) {
        if(! nickName.isEmpty())
            this.nickName = nickName;
    }

    public void updateUser(User user) throws NotDataException {
        if(user == null)
            throw new NotDataException();

        if(user.getUserName()!=null)
            this.setUserName(user.getUserName());
        if(user.getPassword()!=null)
            this.setPassword(user.getPassword());
        if(user.getUserImg()!=null)
            this.setUserImg(user.getUserImg());
        if(user.getNickName()!=null)
            this.setNickName(user.getNickName());
        if(user.getBookshelf()!=null)
            this.setBookshelf(user.getBookshelf());
        if(user.getHistory()!=null)
            this.setHistory(user.getHistory());
        if (user.getHobby() != null)
            this.setHobby(user.getHobby());
        if (user.getUserType() != null)
            this.setUserType(user.getUserType());
        if (user.getLevel() != null)
            this.setLevel(user.getLevel());
        if (user.getBalance() != null)
            this.setBalance(user.getBalance());
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
